// pages/addDizhi/addDizhi.js
import api from '../../utils/api.js'
var app = getApp();
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
import { HTTP } from '../../utils/http.js'
import { config } from '../../utils/config'
let http = new HTTP()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        biaoqian: ["家", "公司", "学校"],
        current: 0,
        sex: 0,
        name: "",
        phone: "",
        xxdz: "",
        status: false,
        lon: "",
        lat: "",
        areaInfo: '',
        zhuangtai: -1,
        town: "",
        tinaxie: false,
        imgUrl: config.imgUrl,
        num: 0,
        show: false,
        selectprovince:'',
        selectcity:'',
        selectarea:'',
        newAddress:'',
        addressMenuIsShow: false,
        animationAddressMenu: {},
    },
    //接口
    //编辑地址的接口
    edit_address(obj) {
        console.log('编辑接口',obj)
        http.request({
            url: api.editAddress,
            data: obj,
            success: res => {
                console.log('99999999999',res)
                if (res.code == 200) {
                    var address = {
                        id: this.data.dizhi.id,
                        address: this.data.newAddress,
                        detail: this.data.xxdz,
                        province:this.data.selectprovince,
                        city:this.data.selectcity,
                        district:this.data.selectarea,
                        name: this.data.name,
                        phone: this.data.phone,
                    }
                    wx.setStorageSync("integralAddress", address)
                    wx.showToast({
                        title: '修改成功',
                        duration: 1500
                    })
                    setTimeout(() => {
                        wx.navigateBack({})
                    }, 1000)
                } else if (res.code == 1003) {
                    wx.showToast({
                        title: '请不要重复提交',
                        icon: 'none',
                        duration: 1500
                    })
                }
            }
        })
    },
    //删除地址的接口
    del_address(obj) {
        http.request({
            url: "/index/del_address",
            data: obj,
            success: res => {
                if (res.code == 200) {
                    wx.showToast({
                        title: '删除成功',
                        duration: 1500
                    })
                    setTimeout(() => {
                        wx.navigateBack({})
                    }, 1000)
                }
            }
        })
    },

    //事件
    //输入姓名
    name(e) {
        this.setData({
            name: e.detail.value
        })
    },

    //更改性别
    changeSex(e) {
        console.log(e.target.dataset.sex)
        this.setData({
            sex: e.target.dataset.sex
        })
    },
    //输入手机号
    phone(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    //输入所在地区
    changeAddress(){
        this.setData({
            show : true
        })
    },
    //输入详细地址
    xxdz(e) {
        var xxdz = e.detail.value
        if (xxdz.indexOf("区") != -1) {
            var xxdz = e.detail.value.split("区")[1]
        }
        this.setData({
            xxdz
        })
    },
    //更改标签
    changeIndex(e) {
        this.setData({
            current: e.target.dataset.idx
        })
    },
    del() {
        let obj = {
            address_id: this.data.dizhi.id,
            user_id: wx.getStorageSync("user_id"),
        }
        this.del_address(obj)
    },
    
    // 保存
    save(e) {
        this.check()
        if (this.data.num == 3) {
            console.log(this.data)
            let dizhi = this.data.dizhi
            let obj = {
                address_id: dizhi.id,
                name: this.data.name,
                user_id: wx.getStorageSync("user_id"),
                sex: this.data.sex,
                mobile: this.data.phone,
                province:this.data.selectprovince ? this.data.selectprovince : this.data.province,
                city:this.data.selectcity ? this.data.selectcity : this.data.city,
                district:this.data.selectarea ? this.data.selectarea : this.data.district,
                address: this.data.newAddress ? this.data.newAddress :this.data.dizhi.address,
                detail: this.data.xxdz,
                type: this.data.current,   // ? this.data.current : this.data.type
                default: 1,
            }
            this.edit_address(obj)
        }
    },
    //保存前的一些校验
    check() {
        var num = 0;
        if (this.data.xxdz) {
            if (this.data.xxdz.length >= 5) {
                num++
            } else {
                wx.showToast({
                    title: "详细地址过短",
                    icon: "none",
                    duration: 1500
                })
            }
        } else {
            wx.showToast({
                title: '请完善您的信息(详细地址)',
                icon: 'none',
                duration: 1500
            })
        }
        if (this.data.phone) {
            if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
                wx.showToast({
                    title: '手机号码有误，请重新填写',
                    icon: 'none',
                    duration: 1500
                })
            } else {
                num++
            }
        } else {
            wx.showToast({
                title: '请完善您的信息(手机号)',
                icon: 'none',
                duration: 1500
            })
        }
        if (this.data.name) {
            if (this.data.name.length <= 10) {
                num++
            } else {
                wx.showToast({
                    title: "请输入真实姓名",
                    icon: "none",
                    duration: 1500
                })
            }
        } else {
            wx.showToast({
                title: '请完善您的信息(姓名)',
                icon: 'none',
                duration: 1500
            })
        }
        this.setData({
            num
        })
    },

    sureSelectAreaListener:function(e){
        var that = this;
        that.setData({
          show: false,
          selectprovince: e.detail.currentTarget.dataset.province,
          selectcity: e.detail.currentTarget.dataset.city,
          selectarea: e.detail.currentTarget.dataset.area,
          newAddress: e.detail.currentTarget.dataset.province +  e.detail.currentTarget.dataset.city +  e.detail.currentTarget.dataset.area
        })
      },

    // 点击所在地区弹出选择框
    select: function(e) {
        //  如果已经显示，不在执行显示动画
        if (this.data.addressMenuIsShow) {
            return false
        } else {
            //  执行显示动画
            this.startAddressAnimation(true)
        }
    },
    //  执行动画
    startAddressAnimation(isShow) {
        if (isShow) {
            //  vh是用来表示尺寸的单位，高度全屏是100vh
            this.animation.translateY(0 + 'vh').step()
        } else {
            this.animation.translateY(40 + 'vh').step()
        }
        this.setData({
            animationAddressMenu: this.animation.export(),
            addressMenuIsShow: isShow,
        })
    },
    //  点击地区选择取消按钮
    cityCancel: function(e) {
        this.startAddressAnimation(false)
    },
    //  点击地区选择确定按钮
    citySure: function(e) {
        var that = this
        var city = that.data.city
        var value = that.data.value
        var city = that.data.city
        var value = that.data.value
        this.startAddressAnimation(false)
            // 将选择的城市信息显示到输入框
        var town = ""
        var city = that.data.citys[value[1]].city_name
        if (city.indexOf("市辖") != -1) {
            town = that.data.provinces[value[0]].province_name
        } else {
            town = that.data.provinces[value[0]].province_name + city
        }
        that.setData({
            town
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        //将地址
        var dizhi = JSON.parse(options.dizhi)
        var phone = dizhi.mobile ? dizhi.mobile : dizhi.phone
        this.setData({
            dizhi,
            name: dizhi.name,
            phone,
            sex : dizhi.sex,
            current: dizhi.type,
            province:dizhi.province,
            city:dizhi.city,
            district:dizhi.district,
            xxdz: dizhi.detail,
            zhuangtai: JSON.parse(options.status),

        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */

    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var animation = wx.createAnimation({
            duration: 1500,
            timingFunction: 'linear',
        })
        this.animation = animation
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    }
})